﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.Controls
{
	public partial class RoleSelector : RepeaterSelector<Guid, List<Role>>
	{
		private const string _showSelectedText = "当前显示选定信息，取消此项将显示所有的信息。";
		private const string _notShowSelectedText = "当前显示所有信息，选择此项将显示选定的信息。";


		#region 属性
		protected override Wuqi.Webdiyer.AspNetPager Pager
		{
			get { return pager; }
		}

		protected override Repeater DataList
		{
			get { return List; }
		}


		public string ShowSelectedText
		{
			get
			{
				object o = ViewState["ShowSelectedText"];
				return o == null ? _showSelectedText : (string)o;
			}
			set { ViewState["ShowSelectedText"] = value; }
		}

		public string NotShowSelectedText
		{
			get
			{
				object o = ViewState["NotShowSelectedText"];
				return o == null ? _notShowSelectedText : (string)o;
			}
			set { ViewState["NotShowSelectedText"] = value; }
		}

		public Roles.RolePropertyCode? ExcludePropertyCode
		{
			get { return (Roles.RolePropertyCode?)ViewState["ExcludePropertyCode"]; }
			set { ViewState["ExcludePropertyCode"] = value; }
		}

		public Guid[] ExcludeRoleIds
		{
			get { return (Guid[])ViewState["ExcludeRoleIds"]; }
			set { ViewState["ExcludeRoleIds"] = value; }
		}
		#endregion

		#region 函数
		protected override PaginationInfo<List<Role>> GetData()
		{
			return Roles.GetPaginationSelectorList(TbKeyword.Text
				, CbShowSelected.Checked ? SelectedItems == null ? new Guid[] { } : SelectedItems.ToArray() : null
				, ExcludePropertyCode
				, ExcludeRoleIds
				, pager.PageSize, pager.CurrentPageIndex);
		}

		/// <summary>
		/// 取得 ListItem 的标识符。
		/// </summary>
		/// <param name="item">列表的 ListItem。</param>
		/// <returns>ListItem 的标识符。</returns>
		protected override Guid GetListItemType(RepeaterItem item)
		{
			Guid identifier;
			HiddenField control;

			control = (HiddenField)item.FindControl("Identifier_RoleId");
			identifier = new Guid(control.Value);

			return identifier;
		}
		#endregion

		protected void CbShowSelected_CheckedChanged(object sender, EventArgs e)
		{
			LoadList();
			if (CbShowSelected.Checked)
				CbShowSelected.Text = ShowSelectedText;
			else
				CbShowSelected.Text = NotShowSelectedText;
		}
	}
}